package com.css.zfzx.fwsjcj.modules.app;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.bpm.platform.org.division.repository.entity.DivisionEntity;
import com.css.bpm.platform.org.user.repository.entity.CertificationEntity;
import com.css.bpm.platform.org.user.repository.entity.UserEntity;
import com.css.bpm.platform.page.login.viewobjects.PwdCheck;
import com.css.bpm.platform.sys.syssubapp.repository.entity.SysSubappEntity;
import com.css.bpm.platform.sys.syssubapp.service.SysSubappService;
import com.css.bpm.platform.utils.PlatformRSAUtils;
import com.css.zfzx.fwsjcj.cache.DivisionCache;
import com.css.zfzx.fwsjcj.commen.util.Constant;
import com.css.zfzx.fwsjcj.modules.app.vo.LoginParamVO;
import com.css.zfzx.fwsjcj.modules.app.vo.UpdatePwdVO;
import com.css.zfzx.fwsjcj.modules.imageupload.service.ImageUploadService;
import com.css.zfzx.fwsjcj.modules.reinforce.entity.FwReinforceEntity;
import com.css.zfzx.fwsjcj.modules.reinforce.service.FwReinforceService;
import com.css.zfzx.fwsjcj.modules.taskmanage.personfootprint.entity.FwPersonFootprintEntity;
import com.css.zfzx.fwsjcj.modules.taskmanage.personfootprint.service.FootPrintService;
import com.css.zfzx.fwsjcj.modules.taskmanage.task.service.TaskService;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.entity.FwCtiyHouseEntity;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.entity.FwNoctiyHouseEntity;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.entity.FwVillageHouseEntity;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.entity.FwVillagePubhouseEntity;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.service.*;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.vo.TaskFormQueryVO;
import com.css.zfzx.fwsjcj.modules.user.entity.FwUserEntity;
import com.css.zfzx.fwsjcj.modules.user.service.FwUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/app")
@Slf4j
@PropertySource(value = "classpath:platform-config.yml")
public class AppController {
    @Autowired
    FwUserService fwUserService;
    @Autowired
    SysSubappService sysSubappService;
    @Value("${androidAppCode}")
    String androidAppCode;
    @Autowired
    TaskService taskService;
    @Autowired
    CityTaskFormService cityTaskFormService;
    @Autowired
    VillagePubHouseFormService villagePubHouseFormService;
    @Autowired
    VillageHouseFormService villageHouseFormService;
    @Autowired
    NoCityTaskFromService noCityTaskFromService;
    @Autowired
    TaskFromService taskFromService;
    @Autowired
    FootPrintService footPrintService;
    @Autowired
    ImageUploadService imageUploadService;
    @Autowired
    private FwReinforceService reinforceService;

    /***
     * 修改密码
     * @param updatePwdVO
     * @return
     */
    @PostMapping("updatePwd")
    public RestResponse updatePwd(HttpServletRequest request, @RequestBody UpdatePwdVO updatePwdVO) {
        String ios = request.getHeader("ios");
        RestResponse restResponse = null;
        SysSubappEntity byCode = sysSubappService.findByCode(androidAppCode);
        String privateKey = null;
        try {
            if ("1".equals(ios)) {
                privateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMfWwCmZe2rPLd8q" +
                        "2GT1cGqOkV4LUv+Xzw4peuMENkYSQrmevmPMz7p0RszW90cWZ1WCZVcz6MbZWS/j" +
                        "keILkZNnjRJRncWSZiqWwDs0QAJdWYFYRQ2oqKDazVzlWirEcXbhXmIlrETCYfvA" +
                        "eWkMNMmQgAMTJkuXeH/sg1v6Fe//AgMBAAECgYEAvG22hcMNxzmRFEOPBSsUk7TY" +
                        "RufAm/Ylq0HbeGFJV6ryNZoEBZBiMnpfI9hyExqB1vP74Ey26YCZgvk+XfjwinVm" +
                        "ieaAnGLmyFbOsQxV84Sa5MAtDfoOj1Za9CzXPaEXsK3HxE8zwuDgJ020oNstC87l" +
                        "gWybYaVt7nuX4zRAr/ECQQDi/7e5FMfYyASYKrcKVv+GBGC+sQoAgaQHX+3kUgUp" +
                        "1yRXzc+2VrwxOd0Wqg8XrnM5wtbAtDyYaujpQcaGUZd5AkEA4V66+N44TzcWtrDk" +
                        "W5gN8gwbKhPRHoUyH1me8c45thrmuZobs0MdU181hPPPKX+NMWUkl1xUM44/M4R4" +
                        "ETdNNwJASl5a87ECNFx7XNsJssKD3oVKqM7ZpwbSFXRxPM8+T9HjXzzXYzaRW1NI" +
                        "wqceYLTPnUnfD608+PZ1rg3Vm6XZiQJBAKcwSXBaGlfZvEXnUGEKmj3X8Ubz0Izd" +
                        "ruNG7vzfSjzoFAXYZ3hC77xrwx5QaHyWnT3plI2c9vIQMp6bRR8wcV0CQGUCQz/2" +
                        "nSNMnHcn7Mqf8Cm/68bWFUK3e25frALjH447Uzp5xaJ4bu9kO0vHEpUs59q/5POk" +
                        "xqI6dh1UV2nWoTs=";
            } else {
                privateKey = byCode.getPrivateKey();
            }
            String odPwd = PlatformRSAUtils.decrypt(updatePwdVO.getOldPwd(), privateKey);
            String nwPwd = PlatformRSAUtils.decrypt(updatePwdVO.getNewPwd(), privateKey);
            CertificationEntity certificationEntity = PlatformAPI.getOrgAPI().getUserAPI().updateCertification(updatePwdVO.getUserId(), odPwd, nwPwd);
            restResponse = RestResponse.succeed(certificationEntity);
        } catch (Exception e) {
            String errormessage = "修改密码失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 查看个人信息
     * @param userId
     * @return
     */
    @GetMapping("userInfo")
    public RestResponse getUserInfo(@RequestParam("userId") String userId) {
        RestResponse response = null;
        try {
            UserEntity user = PlatformAPI.getOrgAPI().getUserAPI().getUser(userId);
            //FwUserEntity fwUserEntity = fwUserService.findByUserId(userId);
            response = RestResponse.succeed(user);
        } catch (Exception e) {
            String errorMessage = "查询信息失败";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * App端登录
     * @param loginParamVO
     * @return
     */
    @PostMapping("doLogin")
    public RestResponse doLogin(HttpServletRequest request, @RequestBody LoginParamVO loginParamVO) {
        String userName = loginParamVO.getUserName();
        String password = loginParamVO.getPassword();
        String appCode = loginParamVO.getAppCode();

        if (StringUtils.isEmpty(userName)) {
            return RestResponse.fail("用户名不能为空");
        }
        if (StringUtils.isEmpty(password)) {
            return RestResponse.fail("密码不能空");
        }
        if (StringUtils.isEmpty(appCode)) {
            return RestResponse.fail("系统编码不能为空");
        }
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            SysSubappEntity byCode = sysSubappService.findByCode(appCode);
            String privateKey = byCode.getPrivateKey();
            PwdCheck pwdCheck = fwUserService.doLogin(userName, password, privateKey);
            HttpSession session = request.getSession();
            if (pwdCheck.isSuccess()) {
                session.setAttribute("userId", pwdCheck.getUserId());
                session.setAttribute("token", pwdCheck.getToken());
                json.put("message", "登陆成功");
                json.put("data", pwdCheck);
                response = RestResponse.succeed(json);
            } else {
                response = RestResponse.fail(pwdCheck.getErrorMessage());
            }
        } catch (Exception e) {
            String errormessage = "登录失败";
            log.error(errormessage, e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }

    /***
     * 注册用户APP
     * @param fwUserEntity
     * @return
     */
    @PostMapping("userApp")
    public RestResponse saveUserApp(@RequestBody FwUserEntity fwUserEntity) {
        if (fwUserEntity == null) {
            return RestResponse.fail("保存用户信息不能为空");
        }
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            if (!StringUtils.isEmpty(fwUserEntity.getUserName())) {
                FwUserEntity byUserName = fwUserService.findByUserName(fwUserEntity.getUserName());
                if (!StringUtils.isEmpty(byUserName)) {
                    return RestResponse.fail("手机号已存在");
                }
            }
            if (!StringUtils.isEmpty(fwUserEntity.getIdNumber())) {
                FwUserEntity byIdNumber = fwUserService.findByIdNumber(fwUserEntity.getIdNumber());
                if (!StringUtils.isEmpty(byIdNumber)) {
                    return RestResponse.fail("身份证号已存在");
                }
            }
            fwUserService.saveUserApp(fwUserEntity);
            json.put("message", "注册成功,请等待管理员审核!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "注册失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
     * 行政区划
     *
     * @return
     */
    @GetMapping("divisions/{divisionid}/subdivisions")
    public RestResponse getDisvion(@PathVariable("divisionid") String divisionId) {
        RestResponse restResponse = null;
        try {
            List<DivisionEntity> subDivisions = PlatformAPI.getDivisionAPI().getSubDivisions(divisionId);
            restResponse = RestResponse.succeed(subDivisions);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 查询任务列表
     *
     * @return
     */
    @GetMapping("pageTask")
    public RestResponse queryTask(@RequestParam("type") String type, @RequestParam("curtPage") int curtPage, @RequestParam("pageSize") int pageSize) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            System.out.println(type);
            jsonObject = taskService.findPage(type, curtPage, pageSize);
            jsonObject.put("message", "查询成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 保存城镇住宅
     *
     * @return
     */
    @PostMapping("{type}/saveCity")
    public RestResponse saveFrom(@PathVariable("type") String type, @RequestBody FwCtiyHouseEntity fwCtiyHouseEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            cityTaskFormService.saveFrom(fwCtiyHouseEntity, type);
            jsonObject.put("message", "保存成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "保存失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     *保存城镇非住宅
     * @param fwNoctiyHouseEntity
     * @return
     */
    @PostMapping("{type}/noCityTask")
    public RestResponse saveFrom(@PathVariable("type") String type, @RequestBody FwNoctiyHouseEntity fwNoctiyHouseEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            noCityTaskFromService.saveForm(fwNoctiyHouseEntity, type);
            jsonObject.put("message", "保存成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "保存失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 农村建筑
     *
     * @param fwVillageHouseEntity
     * @return
     */
    @PostMapping("{type}/villageHouseFrom")
    public RestResponse save(@PathVariable("type") String type, @RequestBody FwVillageHouseEntity fwVillageHouseEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            villageHouseFormService.saveForm(fwVillageHouseEntity, type);
            jsonObject.put("message", "保存成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "保存失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 农村公共建筑
     * @param fwVillagePubhouseEntity
     * @return
     */
    @PostMapping("{type}/villagePubHouse")
    public RestResponse saveForm(@PathVariable("type") String type, @RequestBody FwVillagePubhouseEntity fwVillagePubhouseEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            villagePubHouseFormService.saveForm(fwVillagePubhouseEntity, type);
            jsonObject.put("message", "保存成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "保存失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 查询任务相关的表单
     *
     * @param taskFormQueryVO
     * @param pageSize
     * @return
     */
    @GetMapping("taskForm")
    public RestResponse findFormByTaskId(HttpServletRequest request, TaskFormQueryVO taskFormQueryVO, int curtPage, int pageSize) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = taskFromService.findAll(request, taskFormQueryVO, curtPage, pageSize);
            jsonObject.put("message", "查询成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 查看任务相关的表单信息
     *
     * @return
     */
    @GetMapping("findById")
    public RestResponse findById(String formId, String type) {
        RestResponse restResponse = null;
        try {
            if (Constant.BULIDING_CITY.equals(type)) {
                FwCtiyHouseEntity cityById = cityTaskFormService.findCityById(formId);
                restResponse = RestResponse.succeed(cityById);
            }
            if (Constant.BULIDING_NOCITY.equals(type)) {
                FwNoctiyHouseEntity noCityById = noCityTaskFromService.findNoCityById(formId);
                restResponse = RestResponse.succeed(noCityById);
            }
            if (Constant.BULIDING_VIllAGE.equals(type)) {
                FwVillageHouseEntity villageHouseById = villageHouseFormService.findVillageHouseById(formId);
                restResponse = RestResponse.succeed(villageHouseById);
            }
            if (Constant.BULIDING_VIllAGEPUB.equals(type)) {
                FwVillagePubhouseEntity villagePubHouseById = villagePubHouseFormService.findVillagePubHouseById(formId);
                restResponse = RestResponse.succeed(villagePubHouseById);
            }
        } catch (Exception e) {
            String errormessage = "查看失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 批量提交
     *
     * @param ids
     * @param type
     * @return
     */
    @PostMapping("{type}/subAll/{ids}")
    public RestResponse subAll(@PathVariable("type") String type, @PathVariable("ids") String ids) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isEmpty(ids)) {
            restResponse = RestResponse.fail("勾选不能为空");
        }
        if (StringUtils.isEmpty(type)) {
            restResponse = RestResponse.fail("类型不能为空");
        }
        try {
            String[] split = ids.split(",");
            List<String> list = Arrays.asList(split);
            taskFromService.subAll(list, type);
            jsonObject.put("message", "提交成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "提交失败";
            log.error(errormessage, e);
            RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 修改城镇住宅表单
     *
     * @param type
     * @param fwCtiyHouseEntity
     * @return
     */
    @PostMapping("{type}/updCity")
    public RestResponse updCity(@PathVariable String type, @RequestBody FwCtiyHouseEntity fwCtiyHouseEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            cityTaskFormService.updCity(type, fwCtiyHouseEntity);
            jsonObject.put("message", "修改成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "修改成功";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 保存足迹
     *
     * @param fwPersonFootprintEntity
     * @return
     */
    @PostMapping("saveFoot")
    public RestResponse save(@RequestBody FwPersonFootprintEntity fwPersonFootprintEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            footPrintService.saveFootPrint(fwPersonFootprintEntity);
            jsonObject.put("maessage", "保存成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "保存失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 修改城镇非住宅表单
     *
     * @param type
     * @param fwNoctiyHouseEntity
     * @return
     */
    @PostMapping("{type}/updNoCity")
    public RestResponse updNoctiy(@PathVariable("type") String type, @RequestBody FwNoctiyHouseEntity fwNoctiyHouseEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            noCityTaskFromService.updNoCity(type, fwNoctiyHouseEntity);
            jsonObject.put("message", "修改成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "修改成功";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 修改农村建筑表单
     *
     * @param type
     * @param fwVillageHouseEntity
     * @return
     */
    @PostMapping("{type}/updVillage")
    public RestResponse updVillage(@PathVariable("type") String type, @RequestBody FwVillageHouseEntity fwVillageHouseEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            villageHouseFormService.updVillCity(type, fwVillageHouseEntity);
            jsonObject.put("message", "修改成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "修改成功";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 修改农村公共表单
     *
     * @param type
     * @param fwVillagePubhouseEntity
     * @return
     */
    @PostMapping("{type}/updVillagePub")
    public RestResponse updVillagePub(@PathVariable("type") String type, @RequestBody FwVillagePubhouseEntity fwVillagePubhouseEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            villagePubHouseFormService.updVillPub(type, fwVillagePubhouseEntity);
            jsonObject.put("message", "修改成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "修改成功";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/{ids}/deletAll")
    public RestResponse deletAll(@PathVariable("ids") String ids) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isEmpty(ids)) {
            restResponse = RestResponse.fail("勾选不能为空");
        }
        try {
            String[] split = ids.split(",");
            List<String> list = Arrays.asList(split);
            taskFromService.deletAll(list);
            jsonObject.put("message", "提交成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "提交失败";
            log.error(errormessage, e);
            RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * App端上传图片
     *
     * @param uploadFile
     * @return
     */
    @PostMapping("upload")
    public RestResponse uploadPic(@RequestBody MultipartFile uploadFile) {
        RestResponse restResponse = null;
        try {
            String imageURL = imageUploadService.uploadPic(uploadFile);
            restResponse = RestResponse.succeed(imageURL);
        } catch (Exception e) {
            String errormessage = "上传失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 普查批量上传图片接口
     * @param uploadFile
     * @param buildingCode
     * @return
     */
    @PostMapping("uploadList")
    public RestResponse uploadPicList(MultipartFile uploadFile, String buildingCode) {
        RestResponse restResponse = null;
        try {
            String imageURL = imageUploadService.uploadPicList(uploadFile, buildingCode);
            restResponse = RestResponse.succeed(imageURL);
        } catch (Exception e) {
            String errormessage = "上传失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 获取建筑编码
     *
     * @param divisionNum
     * @return
     */
    @GetMapping("buildingCode")
    public RestResponse getBuildingCode(String divisionNum) {
        RestResponse restResponse = null;
        try {
            String num = DivisionCache.getSerialNum(divisionNum);
            restResponse = RestResponse.succeed(num);
        } catch (Exception e) {
            String errormessage = "获取失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    //-------------------以下是加固工程App接口--------------------------分隔线-----------------------------------------------------//

    /**
     * 新增：暂存或者保存
     *
     * @param projectType     0加固工程，1新建工程
     * @param type            0暂存，1保存
     * @param reinforceEntity
     * @return
     */
    @PostMapping("reinforce/save/{projectType}/{type}")
    public RestResponse save(@PathVariable String projectType, @PathVariable String type, @RequestBody FwReinforceEntity reinforceEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            // 设置项目类型的值
            reinforceEntity.setProjectType(projectType);
            reinforceService.save(type, reinforceEntity);
            jsonObject.put("message", "保存成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "保存失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 修改
     *
     * @param reinforceEntity
     * @return
     */
    @PostMapping("reinforce/update/{type}")
    public RestResponse update(@PathVariable("type") String formType ,@RequestBody FwReinforceEntity reinforceEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            reinforceEntity.setStatus(formType);
            reinforceService.update(reinforceEntity);
            jsonObject.put("message", "修改成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "修改失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 分页
     *
     * @param projectType 0加固工程，1新建工程
     * @param curPage
     * @param pageSize
     * @return
     */
    @GetMapping("reinforce/page")
    public RestResponse getPage(@RequestParam(required = false) String projectType,@RequestParam(required = false) String status ,int curPage, int pageSize) {
        RestResponse restResponse = null;
        try {
            JSONObject jsonObject = reinforceService.getPage(projectType,status,curPage, pageSize);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 查看详情
     *
     * @param id
     * @return
     */
    @GetMapping("reinforce/getone/{id}")
    public RestResponse getOne(@PathVariable String id) {
        RestResponse restResponse = null;
        try {
            FwReinforceEntity one = reinforceService.getOne(id);
            restResponse = RestResponse.succeed(one);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 批量提交
     *
     * @param ids
     * @return
     */
    @PostMapping("reinforce/submit/{ids}")
    public RestResponse submit(@PathVariable("ids") String ids) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<String> idList = Arrays.asList(ids.split(","));
            reinforceService.submit(idList);
            jsonObject.put("message", "提交成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "提交失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("reinforce/del/{ids}")
    public RestResponse delete(@PathVariable("ids") String ids) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<String> idList = Arrays.asList(ids.split(","));
            reinforceService.delete(idList);
            jsonObject.put("message", "删除成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "删除失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 获取工程编号
     *
     * @return
     */
    @GetMapping("reinforce/projectCode")
    public RestResponse getProjectCode() {
        RestResponse restResponse = null;
        try {
            String num = DivisionCache.reinforceCode();
            restResponse = RestResponse.succeed(num);
        } catch (Exception e) {
            String errormessage = "获取失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /**
     * 加固工程上传图片
     * @param multipartFiles
     * @param number
     * @return
     */
    @PostMapping("reinforce/uploadImages")
    public RestResponse uploadImg(MultipartFile[] multipartFiles,String number){
        RestResponse response = null;
        try{
            List<String> imgURL = new ArrayList<>();
            if(multipartFiles!=null && multipartFiles.length > 0 && multipartFiles.length <= 5){
                imgURL = imageUploadService.uploadPics(multipartFiles, number);
                response = RestResponse.succeed(imgURL);
            }else if(multipartFiles.length > 5){
                response = RestResponse.fail("每次上传图片最多不能超过5张!");
            }
        }catch (Exception e){
            String errormessage="上传图片失败";
            log.error(errormessage,e);
            response = RestResponse.fail(errormessage);
        }
        return response;
    }
}
